package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.ui.widget.CheckableImageView;
import com.photoeditor.demo.util.FontUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/9/4 0004.
 */

public class CropAdapter extends BaseAdapter {

    private Context mContext;
    private List<CropBeanManager.CropBean> mData;
    private int selectPos = 0;

    public CropAdapter(Context context) {
        mContext = context;
        mData = CropBeanManager.getCropBeanList();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CropViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_crop, null);
            viewHolder = new CropViewHolder();
            viewHolder.checkBg = (CheckableImageView) convertView.findViewById(R.id.crop_bg);
            viewHolder.cropImage = (ImageView) convertView.findViewById(R.id.crop_image);
            viewHolder.cropText = (TextView) convertView.findViewById(R.id.crop_text);
            FontUtil.setCustomFont(viewHolder.cropText);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (CropViewHolder) convertView.getTag();
        }
        CropBeanManager.CropBean bean = mData.get(position);
        viewHolder.cropImage.setImageResource(bean.getResId());
        viewHolder.cropText.setText(bean.getRatio());
        if (selectPos == position) {
            viewHolder.checkBg.setChecked(true);
        } else {
            viewHolder.checkBg.setChecked(false);
        }
        return convertView;
    }

    public int getSelectPos() {
        return selectPos;
    }


    public void setSelectPos(int selectPos) {
        if (this.selectPos != selectPos) {
            this.selectPos = selectPos;
            notifyDataSetChanged();
        }
    }

    class CropViewHolder {
        CheckableImageView checkBg;
        ImageView cropImage;
        TextView cropText;
    }

}
